package controller;

import model.Card;
import view.UI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class userPlayCardsLister extends JPanel implements MouseListener {
    public Controller controller;
    public MouseLis mouseLis;
    boolean isSingleStraight = true;
    boolean isDoubleStraight = true;
    public userPlayCardsLister(Controller controller) {
        this.controller = controller;
    }

    //遍历桌面集合，画出牌
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int i = 0; i < controller.deskList.size(); i++) {
            g.drawImage(controller.deskList.get(i).getFontImage(), 20 + i * 20, 200, 50, 80, null);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        UI.enterGameJFrame.enterGameJPanel.westSoreJLabel.setText("");
        UI.enterGameJFrame.enterGameJPanel.eastSoreJLabel.setText("");
        UI.enterGameJFrame.enterGameJPanel.yesPlay.setSelected(true);
        //遍历玩家手牌集合，当牌被点击的一瞬间，将牌加入到待出牌集合中
        for (int i = 0; i < controller.getPersonPlayer().getList().size(); i++) {
            Card card = controller.getPersonPlayer().getList().get(i);

            if (card.isClicked()) {
                // 判断待出牌集合中是否已存在相同的牌
                if (!controller.getPersonPlayer().waitPlaylist.contains(card)) {
                    controller.getPersonPlayer().waitPlaylist.add(card);
                  /*  // 清空待出牌集合
                    controller.getPersonPlayer().waitPlaylist.clear();
*/
                } else {
                    /*// 如果待出牌集合中已存在相同的牌，移除该牌
                    controller.getPersonPlayer().waitPlaylist.remove(card);*/
                    // 清空待出牌集合
                    controller.getPersonPlayer().waitPlaylist.clear();
                }

            }
        }
        //从手牌集合移除待出牌里存在的牌
        controller.getPersonPlayer().getList().removeAll(controller.getPersonPlayer().waitPlaylist);
        playCardsLogic();
        controller.getPersonPlayer().waitPlaylist.clear();
    }



    public void playCardsLogic(){
        // 出牌逻辑判断

        // 单牌出牌逻辑
        if (controller.getPersonPlayer().waitPlaylist.size() == 1) {
            // 将待出牌集合的牌加到桌面集合中
            controller.deskList.clear();
            controller.getDeskList().addAll(controller.getPersonPlayer().waitPlaylist);
            // 双牌出牌逻辑
        } else if (controller.getPersonPlayer().waitPlaylist.size() == 2 &&
                controller.getPersonPlayer().waitPlaylist.get(0).getNumber() ==
                        controller.getPersonPlayer().waitPlaylist.get(1).getNumber()) {
            // 将待出牌集合的牌加到桌面集合中
            controller.deskList.clear();
            controller.getDeskList().addAll(controller.getPersonPlayer().waitPlaylist);
            //出三牌型的逻辑
        } else if (controller.getPersonPlayer().waitPlaylist.size() == 3 &&
                controller.getPersonPlayer().waitPlaylist.get(0).getNumber() ==
                        controller.getPersonPlayer().waitPlaylist.get(1).getNumber() && controller.getPersonPlayer().waitPlaylist.get(0).getNumber() ==
                controller.getPersonPlayer().waitPlaylist.get(2).getNumber() && controller.getPersonPlayer().waitPlaylist.get(1).getNumber() ==
                controller.getPersonPlayer().waitPlaylist.get(2).getNumber()) {
            // 将待出牌集合的牌加到桌面集合中
            controller.deskList.clear();
            controller.getDeskList().addAll(controller.getPersonPlayer().waitPlaylist);

            //出三带一牌型
        } else if (controller.getPersonPlayer().waitPlaylist.size() == 4 &&
                controller.getPersonPlayer().waitPlaylist.get(0).getNumber() ==
                        controller.getPersonPlayer().waitPlaylist.get(1).getNumber() && controller.getPersonPlayer().waitPlaylist.get(0).getNumber() ==
                controller.getPersonPlayer().waitPlaylist.get(2).getNumber() && controller.getPersonPlayer().waitPlaylist.get(1).getNumber() ==
                controller.getPersonPlayer().waitPlaylist.get(2).getNumber() && controller.getPersonPlayer().waitPlaylist.get(3).getNumber() != controller.getPersonPlayer().waitPlaylist.get(1).getNumber() &&
                controller.getPersonPlayer().waitPlaylist.get(3).getNumber() != controller.getPersonPlayer().waitPlaylist.get(0).getNumber() &&
                controller.getPersonPlayer().waitPlaylist.get(3).getNumber() != controller.getPersonPlayer().waitPlaylist.get(2).getNumber() || controller.getPersonPlayer().waitPlaylist.size() == 5 && controller.getPersonPlayer().waitPlaylist.get(0).getNumber() ==
                controller.getPersonPlayer().waitPlaylist.get(1).getNumber() && controller.getPersonPlayer().waitPlaylist.get(0).getNumber() ==
                controller.getPersonPlayer().waitPlaylist.get(2).getNumber() && controller.getPersonPlayer().waitPlaylist.get(1).getNumber() ==
                controller.getPersonPlayer().waitPlaylist.get(2).getNumber() && controller.getPersonPlayer().waitPlaylist.get(3).getNumber() != controller.getPersonPlayer().waitPlaylist.get(1).getNumber() &&
                controller.getPersonPlayer().waitPlaylist.get(3).getNumber() != controller.getPersonPlayer().waitPlaylist.get(0).getNumber() &&
                controller.getPersonPlayer().waitPlaylist.get(3).getNumber() != controller.getPersonPlayer().waitPlaylist.get(2).getNumber() && controller.getPersonPlayer().waitPlaylist.get(3).getNumber() == controller.getPersonPlayer().waitPlaylist.get(4).getNumber()) {
            // 将待出牌集合的牌加到桌面集合中
            controller.deskList.clear();
            controller.getDeskList().addAll(controller.getPersonPlayer().waitPlaylist);


            //炸弹的逻辑
        } else if (controller.getPersonPlayer().waitPlaylist.size() == 4 &&
                controller.getPersonPlayer().waitPlaylist.get(0).getNumber() ==
                        controller.getPersonPlayer().waitPlaylist.get(1).getNumber() && controller.getPersonPlayer().waitPlaylist.get(0).getNumber() ==
                controller.getPersonPlayer().waitPlaylist.get(2).getNumber() && controller.getPersonPlayer().waitPlaylist.get(1).getNumber() ==
                controller.getPersonPlayer().waitPlaylist.get(2).getNumber() && controller.getPersonPlayer().waitPlaylist.get(0).getNumber() == controller.getPersonPlayer().waitPlaylist.get(3).getNumber() &&
                controller.getPersonPlayer().waitPlaylist.get(1).getNumber() == controller.getPersonPlayer().waitPlaylist.get(3).getNumber() &&
                controller.getPersonPlayer().waitPlaylist.get(2).getNumber() == controller.getPersonPlayer().waitPlaylist.get(3).getNumber()) {
            // 将待出牌集合的牌加到桌面集合中
            controller.deskList.clear();
            controller.getDeskList().addAll(controller.getPersonPlayer().waitPlaylist);

            //火箭的牌型逻辑
        } else if (controller.getPersonPlayer().waitPlaylist.size() == 2 && controller.getPersonPlayer().waitPlaylist.get(0).getCardName().equals("小王")
                && controller.getPersonPlayer().waitPlaylist.get(1).getCardName().equals("大王")) {
            // 将待出牌集合的牌加到桌面集合中
            controller.deskList.clear();
            controller.getDeskList().addAll(controller.getPersonPlayer().waitPlaylist);


            //单顺出牌
        } else if(isSingleStraight){
            for (int i = 0; i < controller.getPersonPlayer().waitPlaylist.size() - 1; i++) {
                if (controller.getPersonPlayer().waitPlaylist.get(i).getNumber() != controller.getPersonPlayer().waitPlaylist.get(i + 1).getNumber() + 1) {
                }
            }
            // 将待出牌集合的牌加到桌面集合中
            controller.deskList.clear();
            controller.getDeskList().addAll(controller.getPersonPlayer().waitPlaylist);
            //双顺出牌
        }else if(isDoubleStraight){
            if (controller.getPersonPlayer().waitPlaylist.size() % 2 == 0) {
                for (int i = 0; i < controller.getPersonPlayer().waitPlaylist.size() - 2; i += 2) {
                    if (controller.getPersonPlayer().waitPlaylist.get(i).getNumber() !=
                            controller.getPersonPlayer().waitPlaylist.get(i + 1).getNumber() ||
                            controller.getPersonPlayer().waitPlaylist.get(i).getNumber() !=
                                    controller.getPersonPlayer().waitPlaylist.get(i + 2).getNumber() + 1) {
                    }
                }
            }
            // 将待出牌集合的牌加到桌面集合中
            controller.deskList.clear();
            controller.getDeskList().addAll(controller.getPersonPlayer().waitPlaylist);
        }else {
            // 不符合规则，将牌重新加回到手牌集合中
            controller.getPersonPlayer().getList().addAll(controller.getPersonPlayer().waitPlaylist);

        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}